package com.maikang.exammall.api;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.maikang.exammall.entity.dto.ResponseDTO;
import com.maikang.exammall.entity.dto.WxEnterDTO;
import com.maikang.exammall.service.WxEnterService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 微信小程序用户接口
 *
 * @author <a href="https://github.com/binarywang">Binary Wang</a>
 */
@Log4j2
@RequiredArgsConstructor
@RestController
public class WxMaUserController {
    private final WxMaService wxService;
    private final ObjectMapper objectMapper;
    private final WxEnterService wxEnterService;

    /**
     * 登陆接口
     */
    @GetMapping("/ma/login")
    @ApiOperation("不需要传token")
    public ResponseDTO<WxEnterDTO> login(String code) {
        log.debug("wx mini app login: got code {}", code);
        if (StringUtils.isBlank(code)) {
            return ResponseDTO.badRequest("empty jscode");
        }
        try {
            WxMaJscode2SessionResult session = wxService.getUserService().getSessionInfo(code);
            log.info("session is {}", session);
            return ResponseDTO.ok(wxEnterService.maEnter(session));
        } catch (WxErrorException e) {
            log.error(e.getMessage(), e);
            return ResponseDTO.failed(e.getMessage());
        }
    }

    /**
     * <pre>
     * 获取用户信息接口 需要token
     * </pre>
     */
    @SneakyThrows
    @GetMapping("/api/ma-info")
    public ResponseDTO<String> info(String sessionKey, String signature, String rawData, String encryptedData, String iv) {

        // 用户信息校验
        if (!wxService.getUserService().checkUserInfo(sessionKey, rawData, signature)) {
            return ResponseDTO.failed("user check failed");
        }

        // 解密用户信息
        WxMaUserInfo userInfo = wxService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
        wxEnterService.maUserInfo(userInfo);
        return ResponseDTO.ok(objectMapper.writeValueAsString(userInfo));
    }

    /**
     * <pre>
     * 获取用户绑定手机号信息 需要token
     * </pre>
     */
    @SneakyThrows
    @GetMapping("/api/ma-phone")
    public ResponseDTO<String> phone(String sessionKey, String signature, String rawData, String encryptedData, String iv) {
        // 用户信息校验
        if (!wxService.getUserService().checkUserInfo(sessionKey, rawData, signature)) {
            return ResponseDTO.failed("user check failed");
        }
        // 解密
        WxMaPhoneNumberInfo phoneNoInfo = wxService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv);
        wxEnterService.maPhone(phoneNoInfo);
        return ResponseDTO.ok(objectMapper.writeValueAsString(phoneNoInfo));
    }

}
